package com.robot.service;


import com.robot.dao.UserMapper;
import com.robot.model.User;
import com.robot.vo.ResponseMessage;
import lombok.extern.slf4j.Slf4j;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * Created with IntelliJ IDEA.
 * User: li
 * Date: 2019/6/2
 * Time: 17:34
 * PROJECT：robot
 * PACKAGE: com.robot
 *
 * @author:55324936@qq.com To change this template use File | Settings | File Templates.
 */
@Slf4j
@Service
public class UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    ProjectService projectService;
    /**
     * 注册用户
     * @param user
     * @return int
     * **/
    public ResponseMessage registerUser(User user){
        try {
           int i = userMapper.addUser(user);
           if(i !=1){
               return ResponseMessage.errorResponse("注册用户异常");
           }
           projectService.initProject(user);
        }catch (DuplicateKeyException k){
            log.error("用户名重复");
            return ResponseMessage.errorResponse("用户名重复");
        } catch (Exception e){
            log.error("注册用户异常",e);
            return ResponseMessage.errorResponse("注意用户异常");
        }
        return ResponseMessage.successResponse();
    }
    /**
     * 检查用户账号密码是否正确
     * **/
    public ResponseMessage checkUser(User userDo){
        User userDoDb= userMapper.checkUser(userDo);
       if(null ==userDoDb){
          return ResponseMessage.errorResponse("用户名不存在或者密码错误");
       }
       // 一系列计算逻辑呢 ？
       return  ResponseMessage.successResponse(userDoDb);
        //1.登录的用户名是否存在
        //2.登录的用户名密码是否正确

        //直接查询数据库 ，userName , password
        // select * from user where userName =xxx and password =xx
        //如果返回的了一个user的话，那么说明 账号 和密码是正确的， 不知道就是是账户不存在，还是密码错误，账户不存在或者密码错误
        // 因为userName是 唯一的
    }
    /**
     * 获取用户列表为了新增项目成员使用
     * @return ResponseMessage
     * **/
    public ResponseMessage userList(){
        List<User> userList = userMapper.selectUser();
        try{
            return ResponseMessage.successResponse(userList);
        }catch (Exception e){
            log.error("转换用例列表异常",e);
            return ResponseMessage.errorResponse("转换用例列表异常,请联系管理员!");
        }
    }
}
